<template>
  <el-collapse class="question-type-wrapper" v-model="activeNames">
    <el-collapse-item
      v-for="(item, index) of questionMenuConfig"
      :title="item.title"
      :name="index"
      :key="index"
    >
      <draggable
        class="questiontype-list"
        :list="item.questionList"
        :group="{ name: DND_GROUP, pull: 'clone', put: false }"
        :clone="getNewQuestion"
        item-key="path"
      >
        <template #item="{ element }">
          <div class="qtopic-item-outer">
            <div
              :key="element.type"
              class="qtopic-item"
              :id="'qtopic' + element.type"
              @click="onQuestionType({ type: element.type })"
            >
              <!-- @mouseenter="showPreview(element, 'qtopic' + element.type)"
              @mouseleave="isShowPreviewImage = false"
              @mousedown="isShowPreviewImage = false" -->
              <SvgIcon class="x18 mt-step" :name="element.icon" v-if="element.icon.indexOf('qus-')>-1"></SvgIcon>
              <i class="iconfont" :class="['icon-' + element.icon]" v-else-if="element.icon"></i>
              
              <p class="text">{{ element.title }}</p>
            </div>
          </div>
          
        </template>
      </draggable>
    </el-collapse-item>
    <Teleport to="body">
      <div class="preview-popover" v-show="isShowPreviewImage" :style="{ top: previewTop + 'px' }">
        <img :src="previewImg" class="preview-image" />
        <span class="preview-arrow"></span>
      </div>
    </Teleport>
  </el-collapse>
</template>

<script setup>
import questionLoader from '@/materials/questions/questionLoader'
import draggable from 'vuedraggable'
import { DND_GROUP } from '@/management/config/dnd'

import questionMenuConfig, { questionTypeList } from '@/management/config/questionMenuConfig'
import { getQuestionByType } from '@/management/utils/index'
import { useStore } from 'vuex'
import { get as _get, isNumber as _isNumber } from 'lodash-es'
import { computed, ref } from 'vue'
import { QUESTION_TYPE } from '@/common/typeEnum.ts'
import SvgIcon from '@common/SvgIcon/index.vue'
const store = useStore()

const activeNames = ref([0, 1, 2, 3])
const previewImg = ref('')
const isShowPreviewImage = ref(false)
const previewTop = ref(0)
const questionDataList = computed(() => _get(store, 'state.editform.schema.questionDataList'))
const newQuestionIndex = computed(() => {
  const currentEditOne = _get(store, 'state.editform.currentEditOne')
  const index = _isNumber(currentEditOne) ? currentEditOne + 1 : questionDataList.value.length
  return index
})

questionLoader.init({
  typeList: questionTypeList.map((item) => item.type)
})

const getNewQuestion = ({ type }) => {
  const fields = questionDataList.value.map((item) => item.field)
  const newQuestion = getQuestionByType(type, fields)
  let metaData = questionLoader.getMeta(newQuestion.type)
  
  if (metaData?.defaultConfigValue?.title) {
    newQuestion.title = newQuestion.title = `${metaData?.defaultConfigValue?.title}`
  } else {
    newQuestion.title = newQuestion.title = `标题${newQuestionIndex.value + 1}`
  }
  
  if (type === QUESTION_TYPE.VOTE) {
    newQuestion.innerType = QUESTION_TYPE.RADIO
  }

  return newQuestion
}

const onQuestionType = ({ type }) => {
  const newQuestion = getNewQuestion({ type })
  let metaData = questionLoader.getMeta(newQuestion.type)
  
  if (metaData?.defaultConfigValue) {
    Object.assign(newQuestion, { ...metaData.defaultConfigValue })
  }

  store.dispatch('editform/addQuestion', { question: newQuestion, index: newQuestionIndex.value })
  store.commit('editform/setCurrentEditOne', newQuestionIndex.value)
}

// const showPreview = ({ snapshot }, id) => {
//   if (!snapshot) return
//   previewImg.value = snapshot

//   const dragEl = document.getElementById(id)
//   const { top, height } = dragEl.getBoundingClientRect()
//   previewTop.value = top + height / 2

//   isShowPreviewImage.value = true
// }
</script>

<style lang="scss" scoped>
.question-type-wrapper {
  padding: 0;
  border: none;

  :deep(.el-collapse-item__header) {
    font-size: 16px;
    font-weight: bold;
    color: #4a4c5b;
    border-bottom: none;
  }
}

.questiontype-list {
  // height: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  // padding-bottom: 25px;
  overflow-y: scroll;
  overflow-x: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
  border-top: none;

  &::-webkit-scrollbar {
    display: none;
  }

  .qtopic-item-outer {
    padding: 5px;
  }

  .qtopic-item {
    // height: 57px;
    padding: 5px 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 12px;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
    border-radius: 8px;
    background-color: #f3f5f7;

    &:hover {
      background-color: #DFF0FF;
    }

    .text {
      font-size: 12px;
      margin-top: 3px;
      user-select: none;
    }

    .x18 {
      width: 18px;
      height: 18px;
    }
  }

  .iconfont::before {
    font-size: 21px;
    color: $font-color-title;
  }
}
</style>

<style lang="scss">
.qtype-popper-0 {
  transform: translateX(183px);
}

.qtype-popper-1 {
  transform: translateX(106px);
}

.qtype-popper-2 {
  transform: translateX(30px);
}
// 设置拖拽到编辑区的样式
.box .qtopic-item {
  height: 2px;
  width: 100%;
  background-color: var(--primary-color);
  * {
    display: none;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.preview-popover {
  position: fixed;
  left: 390px;
  z-index: 9;
  width: 371px;
  padding: 12px;
  background: white;
  border: 1px solid var(--el-border-color-light);
  box-shadow: var(--el-box-shadow-light);
  transform: translateY(-50%);
  animation: fadeIn 100ms linear forwards;

  .preview-image {
    width: 100%;
    object-fit: contain;
  }

  .preview-arrow {
    position: absolute;
    top: 50%;
    left: -6px;
    height: 10px;
    width: 10px;
    transform: translateX(-50%);
    background: var(--el-border-color-light);
    z-index: -1;
    transform: rotate(-45deg);

    &::before {
      position: absolute;
      content: '';
      height: 10px;
      width: 10px;
      border: 1px solid var(--el-border-color-light);
      background: #ffffff;
      border-bottom-color: transparent;
      border-right-color: transparent;
    }
  }
}
</style>
